<script setup lang="ts">
import { poleWarningDelApi, poleWarningGetListAPi } from '@/apis/rod'
import type { ListParams } from '@/types'
import type { PoleWarningI } from '@/types/rod'
import { formatHandleStatus } from '@/utils/format'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'

const listParams = ref<ListParams>({
  page: 1,
  pageSize: 8,
  poleName: '',
  poleNumber: '',
  handleStatus: undefined,
})
const list = ref<PoleWarningI[]>([])
const total = ref(0)
const getList = async () => {
  const res = await poleWarningGetListAPi(listParams.value)
  list.value = res.rows
  total.value = res.total
}
getList()
const changePage = (newPage: number) => {
  listParams.value.page = newPage
  getList()
}
const changePageSize = (newPageSize: number) => {
  listParams.value.page = 1
  listParams.value.pageSize = newPageSize
  getList()
}
const onSearch = () => {
  listParams.value.page = 1
  listParams.value.pageSize = 8
  getList()
}
const onDel = async (id: number) => {
  console.log('删除')
  await poleWarningDelApi(id)
  ElMessage.success('删除成功~')
}
</script>

<template>
  <div class="page-container">
    <!-- 搜索区域 -->
    <el-form :inline="true">
      <el-form-item label="一体杆名称:">
        <el-input
          placeholder="请输入一体杆名称"
          style="width: 180px"
          v-model="listParams.poleName"
        />
      </el-form-item>
      <el-form-item label="一体杆编号:">
        <el-input
          placeholder="请输入一体杆编号"
          style="width: 180px"
          v-model="listParams.poleNumber"
        />
      </el-form-item>
      <el-form-item label="处置状态:">
        <el-select
          placeholder="请选择处置状态"
          style="width: 180px"
          v-model="listParams.handleStatus"
        >
          <el-option label="未派单" :value="0"></el-option>
          <el-option label="已派单" :value="1"></el-option>
          <el-option label="已接单" :value="2"></el-option>
          <el-option label="已完成" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="list">
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="一体杆名称" align="center" prop="poleName" />
      <el-table-column label="一体杆编号" align="center" prop="poleNumber" />
      <el-table-column label="故障类型" align="center" prop="errorType" />
      <el-table-column label="处置状态" align="center">
        <template #default="{ row }">
          {{ formatHandleStatus(row.handleStatus) }}
        </template>
      </el-table-column>
      <el-table-column label="告警时间" align="center" prop="warningTime" />
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="text" :disabled="row.handleStatus">派单</el-button>
          <el-button
            type="text"
            @click="
              $router.push({
                path: '/pole/waring/detail',
                query: { id: row.id },
              })
            "
            >详情</el-button
          >
          <el-popconfirm title="确定要删除吗?" @confirm="onDel(row.id)">
            <template #reference>
              <el-button type="text" :disabled="row.handleStatus !== 3"
                >删除</el-button
              >
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="listParams.page"
      v-model:page-size="listParams.pageSize"
      layout="total, prev, pager, next, slot, jumper"
      :total="total"
      @current-change="changePage"
    >
      <el-select
        style="width: 95px"
        v-model="listParams.pageSize"
        @change="changePageSize"
      >
        <el-option label="8条/页" :value="8" />
        <el-option label="10条/页" :value="10" />
        <el-option label="15条/页" :value="15" />
      </el-select>
    </el-pagination>
  </div>
</template>

<style lang="scss" scoped>
.page-container {
  padding: 20px;

  .el-form {
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    margin-bottom: 20px;

    ::v-deep(.el-form-item__label) {
      color: rgb(39, 39, 39);
      font-weight: 500;
    }
  }

  .el-table {
    width: 100%;
    height: 400px;

    ::v-deep(.el-table__header th) {
      background-color: rgb(237, 237, 237, 0.9);
      color: rgb(43, 37, 37);
      padding: 10px 0;
    }

    ::v-deep(.el-table__body tr) {
      height: 50px;
    }
  }

  .el-pagination {
    display: flex;
    justify-content: flex-end;
    padding: 20px 0;
  }
}
</style>
